from jili.core.printlog import print
from jili.core.pymsg import mohe
import time
class tick:
    def __init__(self,priceserver=[],isoneserver=True,mohe0=None,auto_stop=["16:20","16:30"],ischecktime=False,code_type="code"):
        self.tq_msgtype = ("priceserver_tq", "subprice")
        self.__ontickcall = {}
        self.tickkey = {}
        self.code_type=code_type
        self.isoneserver=isoneserver
        if priceserver:
            self.priceserver=priceserver
        else:
            self.priceserver=[{"msgtype":("priceserver_tq","price_tq"),"ip":('tcp://127.0.0.1:80001', 'tcp://127.0.0.1:88001')}]
        if not isoneserver:
            self.tq_mohe = []
            for i in self.priceserver:
                if mohe0:
                    mohe0.set_auto_stop(auto_stop)
                    self.tq_mohe.append(mohe0)
                else:
                    self.tq_mohe.append(mohe(i["ip"][0], i["ip"][1], 1, isautorunrecv=True, isnoblock=False,auto_stop=auto_stop))
        else:
            if mohe0:
                mohe0.set_auto_stop(auto_stop)
                self.tq_mohe = mohe0
            else:
                self.tq_mohe=mohe(self.priceserver[0]["ip"][0], self.priceserver[0]["ip"][1], 1, isautorunrecv=True, isnoblock=False,auto_stop=auto_stop)
        self.__ontickcall_all=None
        if ischecktime:
            self.__onallprice=self.__onallprice_True
            self.__onprice=self.__onprice_True
        else:
            self.__onallprice = self.__onallprice_False
            self.__onprice = self.__onprice_False
    def getprice_obj(self,p):
        if self.code_type in p.keys():
            obj = p[self.code_type]
        else:
            obj = p["obj"]
        return obj
    def __onallprice_True(self,msg):
        p = msg["2"]
        obj=self.getprice_obj(p)
        if obj in self.tickkey.keys():
            if self.tickkey[obj] >= p["timekey"]:  # 去重  去老；只认最新行情
                return None
        self.tickkey[obj] = p["timekey"]
        self.__ontickcall_all(p)
    def __onallprice_False(self,msg):
        p = msg["2"]
        self.__ontickcall_all(p)
    def __onprice_True(self,msg):
        p = msg["2"]
        # print("debug",p)
        #p["timekey"] = datetime.datetime.strptime(p["timekey"], '%Y%m%d %H:%M:%S.%f')
        obj=self.getprice_obj(p)
        if obj in self.tickkey.keys():
            if self.tickkey[obj] >= p["timekey"]:  # 去重  去老；只认最新行情
                return None
        self.tickkey[obj] = p["timekey"]
        if obj in self.__ontickcall.keys():
            for i in self.__ontickcall[obj]:
                i(p)
    def __onprice_False(self,msg):
        p = msg["2"]
        # print("debug",p)
        obj=self.getprice_obj(p)
        if obj in self.__ontickcall.keys():
            for i in self.__ontickcall[obj]:
                i(p)
    def subtick(self,obj,ontick,info={}):
        if obj=="all":
            self.__ontickcall_all = ontick
            n = 0
            if not self.isoneserver:
                for i in self.tq_mohe:
                    a=i.subscribe(self.__onallprice, self.priceserver[n]["msgtype"][1], "")
                    print("subscribe",a,n,self.priceserver[n]["msgtype"][1], obj)
                    n = n + 1
            else:
                for i in self.priceserver:
                    a=self.tq_mohe.subscribe(self.__onallprice, i["msgtype"][1], "")
                    print("subscribe",a, n, i["msgtype"][1], obj)
                    n = n + 1

        else:
            if obj not in self.__ontickcall.keys():
                self.__ontickcall[obj]=[]
                self.__ontickcall[obj].append(ontick)
                data={"obj":obj}
                data.update(info)
                n=0
                if not self.isoneserver:
                    for i in self.tq_mohe:
                        a=i.subscribe(self.__ontick, self.priceserver[n]["msgtype"][1], obj)
                        print("subscribe",n,self.priceserver[n]["msgtype"][1], obj)
                        time.sleep(0.1)
                        i.send(self.priceserver[n]["msgtype"][0], "subprice", data)
                        print("send", n, self.priceserver[n]["msgtype"][0], obj)
                        n=n+1
                else:
                    for i in self.priceserver:
                        a=self.tq_mohe.subscribe(self.__ontick, i["msgtype"][1], obj)
                        print("subscribe", n, i["msgtype"][1], obj)
                        time.sleep(0.1)
                        self.tq_mohe.send(i["msgtype"][0], "subprice", data)
                        print("send", n, i["msgtype"][0], obj)
                        n = n + 1
                print("注册行情",obj)
            else:
                if ontick not in self.__ontickcall[obj]:
                    self.__ontickcall[obj].append(ontick)
    def unsubtick(self,obj,ontick=""):
        if obj in self.__ontickcall.keys():
            if ontick == "":
                del self.__ontickcall[obj]
                del self.tickkey[obj]
            else:
                if ontick in self.__ontickcall[obj]:
                    self.__ontickcall[obj].remove(ontick)
                    if len(self.__ontickcall[obj]) == 0:
                        del self.__ontickcall[obj]
                        del self.tickkey[obj]

        # self.tq_mohe.unsubscribe("price_tq", obj,ontick)
        # if self.mh:
        #     self.mh.unsubscribe(self.mh_msgtype, obj,ontick)

def __ontick(t):
    print("__ontick",t)
if __name__ == '__main__':
    priceserver = [
        # {"msgtype": ("priceserver_tq", "price_tq"), "ip": ('tcp://127.0.0.1:80001', 'tcp://127.0.0.1:88001')},
        {"msgtype": ("priceserver", "tick"), "ip": ('tcp://127.0.0.1:80001', 'tcp://127.0.0.1:88001')}
    ]
    tick0=tick(priceserver,False)
    tick0.subtick("all",__ontick)
    a=input("请输入")
    while a!="1":
        a = input("请输入")
        if a == "1":
            break
        else:
            tick0.subtick(a,__ontick)